<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper 
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
	<mapper namespace="${package}.mapping.${table_name}Mapper">
	
	<resultMap type="${raw_table}" id="${table_name}Result">
		<id property="${j_primary_key}" column="${primary_key}" />
		<#list table_columns as column>
			<#if column.isPrimary != "1">
				<result property="${column.dataName}" column="${column.columnName}" />
			</#if>
		</#list>
	</resultMap>
	
		<sql id="columns">
			<#list table_columns as column>${column.columnName}<#if column_has_next>,</#if></#list>
		</sql>
		
		<sql id="insertColumns">
			<#list table_columns as column>#${r"{"}${column.dataName}}<#if column_has_next>,</#if></#list>
		</sql>
	
	    <sql id="selectCondition">
		    <#list table_columns as column>
		    	<if test="${column.dataName} != null ">
					and ${column.columnName} = #${r"{"}${column.dataName}}
				</if>
			</#list>
		</sql>
		
		<sql id ="updateCondition">
			<#list table_columns as column>
				<#if column.isPrimary != "1">
						<if test="${column.dataName} != null ">
				 ${column.columnName} = #${r"{"}${column.dataName}},
						</if>
				</#if>
			</#list>
		</sql>
	
	<!-- insert -->
	<insert id="insert${table_name}" parameterType="${raw_table}">
		<selectKey resultType="${primary_type}" keyProperty="${j_primary_key}" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
		INSERT INTO
		${raw_table}(<include refid="columns" />)
		VALUES(<include refid="insertColumns" />)
	</insert>
	
	<!-- delete -->
	<delete id="delete${table_name}ById" parameterType="long">
		DELETE FROM ${raw_table}  where ${j_primary_key}=#${r"{"}${j_primary_key}}
	</delete>
	
	<!-- update -->
	<update id="update${table_name}ById" parameterType="${raw_table}">
		UPDATE ${raw_table} 
		<set>
		<include refid="updateCondition" />
		</set>
		WHERE ${primary_key}=#${r"{"}${j_primary_key}}
	</update>
	
	<!-- select -->
	<select id="query${table_name}ById" parameterType="java.lang.Long"	resultMap="${table_name}Result">
		SELECT 
		<include refid="columns" />
		FROM ${raw_table} WHERE ${primary_key}=#${r"{"}${j_primary_key}}
	</select>

	<select id="queryPage${table_name}List" parameterType="${package}.query.${table_name}Query"		resultMap="${table_name}Result">
		SELECT 
		<include refid="columns" /> 
		FROM ${raw_table} 
		<where>
			<include refid="selectCondition" /> 
		</where>
	</select>
	
	<select id="query${table_name}List" parameterType="${package}.query.${table_name}Query"		resultMap="${table_name}Result">
		SELECT 
		<include refid="columns" /> 
		FROM ${raw_table} 
		<where>
			<include refid="selectCondition" /> 
		</where>
	</select>

</mapper>